// \u4e00-\u9fa5] 中文验证规则

Page({
  data: {
    id: undefined,
    point: "",
    building: "",
    room: "",
    name: "",
    gender: 1,
    mobile: "",
    idcardFrontUrl: '',
    idcardBackUrl: '',
  },
  onLoad(option) {
    let { point, building, room } = option
    this.setData({
      point,
      building,
      room
    })
    if (!!option.id) {
      this.setData({
        id: option.id
      })
      wx.setNavigationBarTitle({
        title: '修改房屋信息'
      })
      this.getRoomInfo()
    }
  },
  async chooseMedia(event) {
    let { tempFiles } = await wx.chooseMedia({
      count: 1,
      mediaType: 'image'
    })
    let file = tempFiles[0]
    if (file.size > 1 * 1024 * 1024) {
      return wx.showToast({
        title: '图片不能超过1MB',
        icon: "none"
      })
    }
    let res = await wx.http.upload('/upload', {
      filePath: file.tempFilePath,
      name: 'file'
    })

    if (event.mark.idcard == "idcardFrontUrl") {
      this.setData({
        idcardFrontUrl: res.url
      })
    } else {
      this.setData({ idcardBackUrl: res.url })
    }
  },
  async goList() {

    let { __webviewId__, ...data } = this.data

    if (!/^((\d3)|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}/.test(this.data.mobile)) {
      return wx.showToast({
        title: '请填写有效的手机号',
        icon: 'none'
      })
    }

    await wx.http.post('/room', data)

    wx.reLaunch({
      url: '/house_pkg/pages/list/index',
    })
  },
  async getRoomInfo() {
    let res = await wx.http.get('/room/' + this.data.id)
    this.setData({
      point: res.point,
      building: res.building,
      room: res.room,
      name: res.name,
      gender: res.gender,
      mobile: res.mobile,
      idcardFrontUrl: res.idcardFrontUrl,
      idcardBackUrl: res.idcardBackUrl
    })
  },
  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({ [type]: '' })
  },
})
